//
//  DataCenter.swift
//  Gaoloumi
//
//  Created by Jasper on 4/2/16.
//  Copyright © 2016 Jasper. All rights reserved.
//

import UIKit
//import EZSwiftExtensions

class DataCenter: NSObject {
    static let sharedInstance = DataCenter()
    
    var placeholderImage: UIImage?
    var calendar: Calendar?
    
    var commonRegin = [Region]()
    
    override init() {
        let width = (UIScreen.main.bounds.width-8*4)/3
        let oldImge = UIImage(named: "fileicon_pic150")
        placeholderImage = oldImge?.byResize(to: CGSize(width: width, height: width), contentMode: .scaleToFill)
        //        placeholderImage = UIImage.scaleTo(image: oldImge!, w: width, h: width)
    }
    
    func friendlyTime(_ date: Date) -> String {
        let delta = Date().timeIntervalSince(date)
        
        if (delta <= 0) {
            return "刚刚"
        } else if (delta < 60) {
            return "\(Int(delta))秒前"
        } else if (delta < 60*60) {
            return "\(Int(delta/60))分钟前"
        } else if (delta < 60*60*60) {
            return "\(Int(delta/60/60))小时前"
        } else if (delta < 60*60*60*24) {
            return "\(Int(delta/60/60/24))天前"
        } else if (delta < 60*60*60*24*30) {
            return "\(Int(delta/60/60/24/30))月前"
        } else if (delta < 60*60*60*24*30*12) {
            return "\(Int(delta/60/60/24/30/12))年前"
        }
        else {
            if calendar == nil {
                calendar = Calendar.current
            }
            let unitFlags:NSCalendar.Unit = [.year,.month,.day,.hour,.minute]
            let comp = (calendar! as NSCalendar).components(unitFlags, from: Date())
            let currentYear = String(describing: comp.year)
            let currentDay = String(describing: comp.day)
            
            let comp2 = (calendar! as NSCalendar).components(unitFlags, from: date)
            let year = String(describing: comp2.year)
            let month = String(describing: comp2.month)
            let day = String(describing: comp2.day)
            var hour = String(describing: comp2.hour)
            var minute = String(describing: comp2.minute)
            
            if comp2.hour! < 10 {
                hour = "0" + hour
            }
            if comp2.minute! < 10 {
                minute = "0" + minute
            }
            
            if currentYear == year {
                if currentDay == day {
                    return "今天 \(hour):\(minute)"
                } else {
                    return "\(month)月\(day)日 \(hour):\(minute)"
                }
            } else {
                return "\(year)年\(month)月\(day)日 \(hour):\(minute)"
            }
        }
    }
}
